package com.scm.controller;

/**
 * @author CHL
 * @ClassName: VenderController
 * @Description: TODO
 * @date 2022/3/8 14:34
 * @Version 1.0
 */
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scm.dao.VenderMapper;
import com.scm.entity.Vender;
import com.scm.entity.response.ResponseMessage;
import com.scm.util.DateUtil;
import com.scm.util.StringUtil;
import java.util.List;
import org.apache.ibatis.transaction.TransactionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/main/purchase"})
public class VenderController
{
    @Autowired
    private VenderMapper venderMapper;
    private ResponseMessage rm;

    public boolean checkVender(Vender vender)
    {
        if (StringUtil.isEmpty(vender))
        {
            this.rm.setMessage("用户信息不能为空");
            return false;
        }
        String venderCode = vender.getVenderCode();
        if (StringUtil.isEmpty(venderCode))
        {
            this.rm.setMessage("此供应商编号不能为空");
            return false;
        }
        if (!venderCode.matches("^[a-zA-Z0-9]{4,20}$"))
        {
            this.rm.setMessage("供应商编号要求4-20数字字母");
            return false;
        }
        String name = vender.getName();
        if (StringUtil.isEmpty(name))
        {
            this.rm.setMessage("名称name不能为空");
            return false;
        }
        if (name.length() > 100)
        {
            this.rm.setMessage("名称name超过100个字符");
            return false;
        }
        String password = vender.getPassWord();
        if (StringUtil.isEmpty(password))
        {
            this.rm.setMessage("密码不能为空");
            return false;
        }
        if ((password.length() > 20) || (password.length() < 4))
        {
            this.rm.setMessage("密码超过长度");
            return false;
        }
        String address = vender.getAddress();
        if (StringUtil.isEmpty(address))
        {
            this.rm.setMessage("地址不能为空");
            return false;
        }
        String createDate = vender.getCreateDate();
        if (StringUtil.isEmpty(createDate))
        {
            this.rm.setMessage("创建时间不能为空");
            return false;
        }
        if (!DateUtil.isValid(createDate))
        {
            this.rm.setMessage("时间格式不对");
            return false;
        }
        String tel = vender.getTel();
        if (StringUtil.isEmpty(tel))
        {
            this.rm.setMessage("电话不能为空");
            return false;
        }
        return true;
    }

    @RequestMapping({"/vender/add"})
    public ResponseMessage venderAdd(Vender vender)
    {
        this.rm = new ResponseMessage();
        boolean flag = checkVender(vender);
        if (!flag)
        {
            this.rm.setCode(Integer.valueOf(3));
            return this.rm;
        }
        this.venderMapper.addVender(vender);
        this.rm.setCode(Integer.valueOf(2));
        this.rm.setMessage("新增成功");
        return this.rm;
    }

    @RequestMapping({"/vender/delete"})
    public ResponseMessage delete(String venderCode)
    {
        this.rm = new ResponseMessage();
        if (venderCode == null)
        {
            this.rm.setCode(Integer.valueOf(3));
            this.rm.setMessage("缺少入参");
        }
        else
        {
            int n = this.venderMapper.deleteVender(venderCode);
            if (n == 0) {
                throw new TransactionException("供应商编号不存在或删除失败");
            }
            this.rm.setCode(Integer.valueOf(2));
            this.rm.setMessage("删除成功");
            this.rm.setData(this.venderMapper.select(null));
        }
        return this.rm;
    }

    @RequestMapping({"/vender/update"})
    public ResponseMessage update(Vender vender)
    {
        this.rm = new ResponseMessage();
        boolean flag = checkVender(vender);
        if (!flag)
        {
            this.rm.setCode(Integer.valueOf(3));
            return this.rm;
        }
        int n = this.venderMapper.updateVender(vender);
        if (n == 0)
        {
            this.rm.setCode(Integer.valueOf(4));
            this.rm.setMessage("修改失败");
        }
        else
        {
            this.rm.setCode(Integer.valueOf(2));
            this.rm.setMessage("修改成功");
        }
        return this.rm;
    }

    @RequestMapping({"/vender/show"})
    public PageInfo<Vender> select(Vender vender, Integer page)
    {
        PageHelper.startPage(page == null ? 1 : page.intValue(), 10);
        List<Vender> venders = this.venderMapper.select(vender);
        PageInfo<Vender> info = new PageInfo(venders);
        return info;
    }

    @RequestMapping({"/vender/all"})
    public List<Vender> selectAll()
    {
        List<Vender> venders = this.venderMapper.select(null);
        return venders;
    }
}

